import wepy from 'wepy'
import Cache from '../utils/Cache'
import Firewall from '../utils/Firewall'
import NTPromise from '../utils/NTPromise'
const host = 'https://www.damengxiang.me'

// const wxRequest = async (url, params = {}) => {
//   params = Object.assign({toast: true}, params)
//   if(params.toast){
//     wx.showToast({
//       title: '加载中...',
//       icon: 'loading'
//     })
//   }
//   console.log('wxRequest : ', url, params)
//   const firewall = new Firewall()
//   let authed = await firewall.access(url)
//   if (!authed) {
//     console.log('auth failed : ', authed)
//     return {data: {success: false}}
//   }
//   console.log('auth success : ', authed, host+url)
//   let session = new Cache().getLocalSession()
//   let res = wepy.request({
//     url:  host + url,
//     method: params.method || 'GET',
//     data: params.data || {},
//     header: Object.assign({'Content-Type': 'application/json', 'x-session-id': session.sid}, params.header) ,
//   })

//   if(params.toast){
//     wx.hideToast()
//   }
//   return res
// }

const firewall = new Firewall()
const wxRequest = async (url, params) => {
  params = Object.assign({toast: false}, params)
  if(params.toast){
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })
  }

  let authed = await firewall.access(url)
  if (!authed) {
    return {data: {success: false, message:'auth failed'}}
  }

  let session = new Cache().getLocalSession()
  return new Promise((resolve, reject) => {
    wx.request({
      url: host + url,
      method: params.method || 'GET',
      data: params.data,
      header: Object.assign({'Content-Type': 'application/json', 'x-session-id': session.sid}, params.header),
      success: (res) => {
        params.toast && wx.hideToast()
        resolve(res)
      },
      fail: (res) => {
        params.toast && wx.hideToast()
        reject(res)
      }
    })
  })
}

const imgUrl = function(path){
  if( path && path.indexOf('public://') === 0) {
    return host+ '/files/'+path.split('//')[1]
  }
  return path
}

//API 按照模块区分,方便归类

//pages/app
const getXcxDetail = (params) => wxRequest('/api/app/' + params.query.hashid)

//pages/homepage
const getSelectFeatures = () => wxRequest('/api/index/features')
const getSelectedXcxs = () => wxRequest('/api/index/selected_apps')
const getIndexApps = () => wxRequest('/api/index/apps')

//pages/feature
const getFeatureDetail = (params) => wxRequest('/api/feature/'+ params.query.hashid)
const getFeatureList = () => wxRequest('/api/features')

//pages/category
const getCategoryDetail = (params) => wxRequest('/api/categories/'+params.query.cat,params)

//favorite
const favorite  = (params) => wxRequest(`/api/my/favorite/${params.query.type}/${params.query.hashid}`,Object.assign({method:'POST'}, params))
const unfavorite  = (params) => wxRequest(`/api/my/favorite/${params.query.type}/${params.query.hashid}`, Object.assign({method:'DELETE'}, params))
const getFavoriteList  = (params) => wxRequest(`/api/my/favorites/${params.query.type}`, params)

//search
const xcxSearch = (params) => wxRequest('/api/apps', params)

module.exports = {
  wxRequest,
  getXcxDetail,
  getSelectFeatures,
  getSelectedXcxs,
  getIndexApps,
  imgUrl,
  getFeatureDetail,
  getFeatureList,
  favorite,
  unfavorite,
  getCategoryDetail,
  getFavoriteList,
  xcxSearch
}
